package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import cc.edt.iceutils3.date.IceDateOperationUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 佣金服务类实现
 * @author sl
 */
@Service
@Slf4j
public class CommissionServiceImpl implements CommissionService {



    @Resource
    private SelectFirstMoneyService selectFirstMoneyService;
    @Resource
    private ComputeService computeService;
    @Resource
    private OrderCreditCKService orderCreditCKService;

    @Resource
    private OrderChannelService orderChannelService;
    @Resource
    private MerchantService merchantService;



    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void setCommissiont(OrderChannel orderChannel) {
        if(orderChannel != null){
            ActionResult result = selectFirstMoneyService.selectFirstMoneyCk(orderChannel);
            if(result.isSuccess()){
                OrderCreditCK orderCreditCK = (OrderCreditCK) result.getResultList();
                if(orderCreditCK != null){
                    if(IceStringUtils.isBlank(orderCreditCK.getChannelId())){
                        orderChannelService.setExpansionTypeAndLevl(orderCreditCK);
                    }

                    if(orderCreditCK.getOverState() != CommonConstant.FLOW_OVER){
                        if(orderChannel.getSettletType() != null){
                            if(CommonConstant.SETTLET_TYPE_YJ_STR.equals(orderChannel.getSettletType())){
                                orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                                orderCreditCK.setRemark("CK 签约月结订单不计算佣金");
                            }
                        }else{

                            Integer settletType = merchantService.selectSettletTypeByExapnsionId(orderCreditCK.getChannelId());
                            if(CommonConstant.STAFF_TYPE_QIANY == orderCreditCK.getChannelType() && settletType != null
                                    && settletType.equals(CommonConstant.SETTLET_TYPE_YJ) ){
                                orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                                orderCreditCK.setRemark("CK 签约月结订单不计算佣金");
                            }
                        }
                        if(IceDateOperationUtils.betweenDays(orderCreditCK.getOrderDate(),orderCreditCK.getFirstDate()) > 60){
                            orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                            orderCreditCK.setRemark("CK 首充时间比订单时间大了 60天以上");
                        }else{
                            computeService.computCommissionCK(orderCreditCK);
                            if(CommonConstant.IS_MONTH.equals(orderCreditCK.getIsMonth())){
                                orderCreditCK.setRemark("CK按月发匹配成功首次发放");
                            }else{
                                if(orderCreditCK.getCommissionMoney() != null){
                                    orderCreditCK.setOverState(CommonConstant.FLOW_OVER);
                                    orderCreditCK.setRemark("CK 计算佣金成功");
                                }else{
                                    orderCreditCK.setOverState(CommonConstant.FLOW_NOT_OVER);
                                    orderCreditCK.setRemark("CK 没有匹配到规则");
                                }
                            }

                        }
                    }

                    orderCreditCK.setAddtime(new Date());
                    orderCreditCKService.saveOrUpdateOrderCreditCk(orderCreditCK);

                }
            }
        }
    }

    @Override
    public void moveOrderCreditCkToOrderCredit() {
        orderCreditCKService.moveOrderCreditCkToOrderCredit();
    }



    @Override
    public void moveOrderCreditCkToOrderCreditByOrderCode(String orderCode) {
        orderCreditCKService.moveOrderCreditCkToOrderCreditByOrderCode(orderCode);
    }



}
